<?php
include_once(dirname(__FILE__)."/../../libs/dbclass/db_class.php");
class dbconn extends mysql{
//获取多条记录
 function ds($sql)
	{
		$query = parent::query($sql);
		$rs=array();
		$row=array();
		$rs=parent::fetch_assoc();
		$i=0;
		if(parent::db_num_rows()>0)
		{
			
			do
			{
				$rs['index']=$i;
				array_push($row,$rs);
				$i=$i+1;
			}while($rs=parent::fetch_assoc($query));
		}
		parent:: free();
		return $row;
	}
	//获取一条记录
	function dvstr($sql)
	{
		$query = parent::query($sql);
		$rs=parent::fetch_assoc();
		//echo $this->pconn;
	 
		parent:: free();
		 
		return $rs;
	}
	//执行更新、删除、插入
	 function cmd($sql)
	{
		$query = parent::query($sql);
		 
		return $query ;
	}
	
	
	/*获得分页sql语句
	* $col 查询内容
	* $page1 当前的页面
	*/
	function pagersql($col,$tb,$where,$order,$pagecount1,$pagesize1,$page1)
	{
       
		if($pagecount1<=0){
			//return "null";
			$startid1=1;
		}else{
				
			if ($page1 < 1 || $page1 > $pagecount1){
				$page1 = 1;
			 }
			$startid = ($page1 - 1) * $pagesize1;
			$startid1 = $startid + 1;
		}
		
		if(trim($where)==""){
			$where="";
		}else{
			$where="where ".$where;
		}
		
		$sqlstr="select ".$col." from ".$tb." ".$where." ".$order." LIMIT ".($startid1-1).",".$pagesize1;

		return $sqlstr;
	}
	
	//获得分页内容的数组
	function pagerds($col,$tb,$where,$order,$pagecount1,$pagesize1,$page1)
	{
		          
		return $this->ds($this->pagersql($col,$tb,$where,$order,$pagecount1,$pagesize1,$page1));
		//return $this->pagersql($col,$tb,$where,$order,$pagecount1,$pagesize1,$page1);
	}
	
	
	//获得查询记录总条数
	function getcount($col,$tb,$where)
	{
		if(trim($where)=="")
		{
			$where="";
		}
		else
		{
			$where="where ".$where;
		}
		$sqlstr="select count(".$col.") as kd from ".$tb." ".$where;
		$rs=$this->dvstr($sqlstr);
		return $rs["kd"];
		
	}
	
	function slsql($key,$zi,$gx1="or",$gx2="or", $schar1=" ",$schar2=",")
	{
		if($key=="")
		{
			return "";
		}
		$str = "";
		$ziduan = "";
		$fg = $schar1;
		$fg1 = $schar2;
		$outstr = "";
		$guanxi = "";
		$guanxi1 = "";
		$k1 = "";
		$k2 = "";
		$xxx="";
		$x="";
		$f=0;
		$i=0;
		$f1=0;
		$i1=0;
		$str = $key;
		$ziduan = $zi;
		$guanxi = " ".$gx1." ";
		$guanxi1 = ") ".$gx2." (";
		if (trim($str) != "")
		{
			$k1 = "(";
			$k2 = ")";
		}
		if ($ziduan == "")
		{
			return "";
		}
		if ($str == "")
		{
			$guanxi1 = "";
		}
		$x = split($fg1,$ziduan);
		$f1 = (count($x));
		for ($i1 = 0; $i1 < $f1; $i1++)
		{
			if ($i1 == $f1 - 1)
			{
				$guanxi1 = "";
			}
			$xxx = split($fg,$str);
			$f = (count($xxx));
			for ($i = 0; $i<$f; $i++)
			{
				if (($i1) > 0)
				{
					$guanxi = " ".$gx1." ";
				}
				if (($i) == ((count($xxx) - 1)))
				{
					$guanxi = "";
				}
				if (trim($xxx[$i]) != "")
				{
					$outstr = $outstr.trim($x[$i1])." like " . "'%" .trim($xxx[$i]) . "%'" . $guanxi;
				}
			}
			$outstr = $outstr . $guanxi1;
		}
		$outstr = $k1 . $outstr . $k2;
		return "(".(trim($outstr)).")";
		//$outstr = "";
	}
 
	function getmatchstr($patten,$str,$arraylist)
	{
		if(preg_match_all($patten,$str,$arraylist)){
			return $arraylist;
		}else{
			return "";
		}
		//if(preg_match_all("/http:\/\/[a-zA-Z0-9][a-zA-Z0-9\/\._-]+[\.html]/",$str,$arraylist))
		//{
		//foreach ($matches[0] as $url)
		//	{
		//echo $url.'<br>';
		//}
		//}else{
		//echo '没有合适';
		//}
	}
	
	
	function CheckSql($p,$g)
	{
		/***************************************************************
		防注入函数：
		$SqlStr:防止输入的字符串。
		调用实例:CheckSql($_POST,$_GET);。
		***************************************************************/
		$checkStr=array("select","delete","update","create","destory","drop","alter","and","or","like","exec","count","*","chr","mid","master","truncate","char","declare",";","-","+");
		$StrNum=count($checkStr);
		foreach($p as $post)
		{
			for($i=0;$i<$StrNum;$i++)
			{
				if(strstr(strtolower($post),$checkStr[$i]))
				$inNum=true;
			}
		}
		if($inNum)
		{
			global $mc;
			$mc->msg("请勿输入非法字符","go");
			exit;
		}
	}

/*录入资料*/
function add_data($table,$dataArray){
	 if($dataArray==null){
	 	return "array is null";
	 }
	while(list($key,$val)=each($dataArray)) {
			$field .="$key,";
			$value .="'$val',";
		}
		$field = substr( $field,0,-1);
		$value = substr( $value,0,-1);
		$sql = "insert into $table($field) values($value)";
		return $this->cmd($sql);
	
}

/*更新资料*/
function update_data($table,$dataArray,$where=""){
	 if($dataArray==null){
	 	return "array is null";
	 }
	while(list($key,$val)=each($dataArray)) {
			//$field .="$key,";
			//$value .="'$val',";
			$strs .="$key='$val',";
		}
		//$field = substr( $field,0,-1);
		$strs = substr( $strs,0,-1);
		if($where==""){
			$sql = "update $table set $strs ";
		}else{
			$sql = "update $table set $strs where $where";
		}
		
		return $this->cmd($sql);
	
}

	/*查询表是否有记录
	* $table  表名
	* $data 参数
	* $where  条件
	* true  有记录
	*/
	function is_have($table,$data="",$where=""){
		
		if($data==""){
			$data='*';
		}
		
		if($where!=""){
			$where="where ".$where;
		}
	    $sql = "select $data from $table  $where ";
		$rs=$this->dvstr($sql);
	
		if(!empty($rs)>0){
			
			return true;
		}else{
			return false;
		}
	}
	
}	

?>